﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HR60_PanelSoft.Tools
{
    class ByteUils
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="sep">分隔符</param>
        /// <returns></returns>
        public static string byte2HexString(byte[] bytes,string sep = ""){

             StringBuilder ret = new StringBuilder();
            foreach (byte b in bytes)
            {
                //{0:X2} 大写
                ret.AppendFormat("{0:X2}", b);
                ret.Append(sep);
          
                
            }
            var hex = ret.ToString();
           return hex;

        }


        public static byte[] string2hex(string s)
        {
            int i =0;
            byte[] hexs = new byte[s.Length / 2];
            int hex_i = 0;
            while((i+2)<=s.Length){
               
               string hex =  s.Substring(i, 2);
               hexs[hex_i++] = Convert.ToByte(hex, 16);
               i = i + 2;
            }

            return hexs;

        }

        public static byte[] ip2hex(string s)
        {
          
            byte[] hexs = new byte[4];
            string[] array= s.Split('.');
            int hex_i = 0;
            foreach (string _s in array)
            {
                hexs[hex_i++] = Convert.ToByte(_s, 10);
            }
            
            return hexs;

        }




        public static string byte2Binary(byte b,int padding)
        {
            string binary = Convert.ToString(b, 2);
            return binary.PadLeft(padding, '0');
        }




        public static byte[] china2Hex(string china)
        {
           return  Encoding.GetEncoding("GBK").GetBytes(china);
            
        }
    }
}
